{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #无人机方阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #counting #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #计数 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minimumSwitchingTimes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #无人机方阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "在 「力扣挑战赛」 开幕式的压轴节目 「无人机方阵」中，每一架无人机展示一种灯光颜色。 无人机方阵通过两种操作进行颜色图案变换：\n",
    "- 调整无人机的位置布局\n",
    "- 切换无人机展示的灯光颜色\n",
    "\n",
    "\n",
    "给定两个大小均为 `N*M` 的二维数组 `source` 和 `target` 表示无人机方阵表演的两种颜色图案，由于无人机切换灯光颜色的耗能很大，请返回从 `source` 到 `target` 最少需要多少架无人机切换灯光颜色。\n",
    "\n",
    "\n",
    "**注意：** 调整无人机的位置布局时无人机的位置可以随意变动。\n",
    "\n",
    "\n",
    "**示例 1：**\n",
    "> 输入：`source = [[1,3],[5,4]], target = [[3,1],[6,5]]`\n",
    ">\n",
    "> 输出：`1`\n",
    ">\n",
    "> 解释：\n",
    "> 最佳方案为\n",
    "将 `[0,1]` 处的无人机移动至 `[0,0]` 处；\n",
    "将 `[0,0]` 处的无人机移动至 `[0,1]` 处；\n",
    "将 `[1,0]` 处的无人机移动至 `[1,1]` 处；\n",
    "将 `[1,1]` 处的无人机移动至 `[1,0]` 处，其灯光颜色切换为颜色编号为 `6` 的灯光；\n",
    "因此从`source` 到 `target` 所需要的最少灯光切换次数为 1。\n",
    ">![8819ccdd664e91c78cde3bba3c701986.gif](https://pic.leetcode-cn.com/1628823765-uCDaux-8819ccdd664e91c78cde3bba3c701986.gif){:height=300px}\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**示例 2：**\n",
    "> 输入：`source = [[1,2,3],[3,4,5]], target = [[1,3,5],[2,3,4]]`\n",
    ">\n",
    "> 输出：`0`\n",
    "> 解释：\n",
    "> 仅需调整无人机的位置布局，便可完成图案切换。因此不需要无人机切换颜色\n",
    "\n",
    "\n",
    "**提示：**\n",
    "`n == source.length == target.length`\n",
    "`m == source[i].length == target[i].length`\n",
    "`1 <= n, m <=100`\n",
    "`1 <= source[i][j], target[i][j] <=10^4`\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [0jQkd0](https://leetcode.cn/problems/0jQkd0/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [0jQkd0](https://leetcode.cn/problems/0jQkd0/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,3],[5,4]]\\n[[3,1],[6,5]]', '[[1,2,3],[3,4,5]]\\n[[1,3,5],[2,3,4]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        \n",
    "        def is_in(source: List[List[int]],v):\n",
    "            for i in range(len(source)):\n",
    "                if v in source[i]:\n",
    "                    source[i].remove(v)\n",
    "                    return v\n",
    "            return None\n",
    "                \n",
    "\n",
    "\n",
    "        count = 0\n",
    "        for t in target:\n",
    "            for value in t:\n",
    "                if is_in(source,value) is None:\n",
    "                    count+=1\n",
    "        \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        ans = 0\n",
    "        p = [0 for _ in range(10006)]\n",
    "        m, n = len(source), len(source[0])\n",
    "        for i in range(m * n):\n",
    "            p[source[i//n][i%n]] += 1\n",
    "            p[target[i//n][i%n]] -= 1\n",
    "        \n",
    "        \n",
    "        for i in range(10006):\n",
    "            if p[i] > 0: ans += p[i]\n",
    "    \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        n = len(source)\n",
    "        m = len(source[0])\n",
    "        sum = 0\n",
    "        li = []\n",
    "        for i in range(10001):\n",
    "            li.append(0)\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                li[source[i][j]] += 1\n",
    "                li[target[i][j]] -= 1\n",
    "        for i in li:\n",
    "            if i>0:\n",
    "                sum += i\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        s=[]\n",
    "        t=[]\n",
    "        n=len(source)\n",
    "        m=len(source[0])\n",
    "        for i in range(n):\n",
    "            s+=source[i]\n",
    "            t+=target[i]\n",
    "\n",
    "        count=0\n",
    "        for i in s:\n",
    "            if i in t:\n",
    "                t.remove(i)\n",
    "            else:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        n = len(source)\n",
    "        m = len(source[0])\n",
    "        source_helper = [0] * (n * m)\n",
    "        target_helper = [0] * (n * m)\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                source_helper[i * m + j] = source[i][j]\n",
    "                target_helper[i * m + j] = target[i][j]\n",
    "        source_helper.sort()\n",
    "        target_helper.sort()\n",
    "        si, ti = 0, 0\n",
    "        while(si < len(source_helper) and ti < len(target_helper)):\n",
    "            if source_helper[si] == target_helper[ti]:\n",
    "                source_helper.pop(si)\n",
    "                target_helper.pop(ti)\n",
    "            elif source_helper[si] < target_helper[ti]:\n",
    "                si += 1\n",
    "            else:\n",
    "                ti += 1\n",
    "        return len(source_helper)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        ans = [0] * 10001\n",
    "        temp = 0\n",
    "        for i in range(len(source)):\n",
    "            for j in range(len(source[0])):\n",
    "                ans[source[i][j]] += 1\n",
    "                ans[target[i][j]] -= 1\n",
    "        for num in ans:\n",
    "            if num > 0:\n",
    "                temp += num\n",
    "        return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c = Counter()\n",
    "        for i in range(len(target)):\n",
    "            for j in range(len(target[0])):\n",
    "                c[target[i][j]] += 1\n",
    "        ans = 0\n",
    "        for i in range(len(source)):\n",
    "            for j in range(len(source[0])):\n",
    "                if c.get(source[i][j]):\n",
    "                    c[source[i][j]] -= 1\n",
    "                else:\n",
    "                    ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        dic={}\n",
    "        for i in range(len(source)):\n",
    "            for j in range(len(source[0])):\n",
    "                if source[i][j] not in dic:\n",
    "                    dic[source[i][j]]=1\n",
    "                else:\n",
    "                    dic[source[i][j]]+=1\n",
    "        res=0\n",
    "        for i in range(len(source)):\n",
    "            for j in range(len(source[0])):\n",
    "                if target[i][j] not in dic:\n",
    "                    res+=1\n",
    "                else:\n",
    "                    dic[target[i][j]]-=1\n",
    "                    if dic[target[i][j]]==0:\n",
    "                        del dic[target[i][j]]\n",
    "        for d in dic:\n",
    "            res+=dic[d]\n",
    "        \n",
    "        return res//2\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        a=sum(target,[])\n",
    "        for i in sum(source,[]):\n",
    "            if i in a:\n",
    "                a.remove(i)\n",
    "        return len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        n, m = len(source), len(source[0])\n",
    "        cnt = Counter()\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                cnt[source[i][j]] += 1\n",
    "        \n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if cnt[target[i][j]]:\n",
    "                    cnt[target[i][j]] -= 1\n",
    "                else:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        HashMap = collections.defaultdict(int)\n",
    "        for i in range(len(source)):\n",
    "            for j in range(len(source[0])):\n",
    "                HashMap[source[i][j]] += 1\n",
    "                HashMap[target[i][j]] -= 1\n",
    "        ans = 0\n",
    "        for _, val in HashMap.items():\n",
    "            ans += abs(val)\n",
    "        return ans // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "       source_line=[]\n",
    "       target_line=[]\n",
    "       for i in range(len(target)):\n",
    "            source_line+=source[i]\n",
    "            target_line+=target[i]\n",
    "       type_1=[0]*10004\n",
    "       type_2=[0]*10004\n",
    "       for i in range(len(source_line)):\n",
    "           type_1[source_line[i]]+=1\n",
    "           type_2[target_line[i]]+=1\n",
    "       ans=0\n",
    "       for i in range(len(type_1)):\n",
    "           if type_2[i]-type_1[i]>0:\n",
    "               ans+=type_2[i]-type_1[i]\n",
    "       return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        d = {}\n",
    "        for x in source:\n",
    "            for y in x:\n",
    "                if y in d:\n",
    "                    d[y] += 1\n",
    "                else:\n",
    "                    d[y] = 1\n",
    "        for x in target:\n",
    "            for y in x:\n",
    "                if y in d:\n",
    "                    d[y] -= 1\n",
    "                    if d[y] == 0:\n",
    "                        del d[y]\n",
    "                else:\n",
    "                    pass\n",
    "        #print(d)\n",
    "        return sum(d.values())\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        result = 0\n",
    "        source_num = []\n",
    "        for i in source:\n",
    "            source_num.extend(i)\n",
    "        target_num = []\n",
    "        for i in target:\n",
    "            target_num.extend(i)\n",
    "        for i in source_num:\n",
    "            if i not in target_num:\n",
    "                result += 1\n",
    "            else:\n",
    "                target_num.remove(i)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        s={}\n",
    "        sum0=0\n",
    "        for i in source:\n",
    "            for j in i:\n",
    "                s[j]=s.get(j,0)+1\n",
    "        for i in target:\n",
    "            for j in i:\n",
    "                if j not in s:\n",
    "                    sum0+=1\n",
    "                else:\n",
    "                    s[j]-=1\n",
    "        for i in s:\n",
    "            if s[i]<0:\n",
    "                sum0+=abs(s[i])\n",
    "        return sum0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "\n",
    "        d = defaultdict(int)\n",
    "\n",
    "        for row in source:\n",
    "            for n in row:\n",
    "                d[n] += 1\n",
    "        \n",
    "        for row in target:\n",
    "            for n in row:\n",
    "                if d[n] > 0:\n",
    "                    d[n] -= 1\n",
    "        \n",
    "        return sum(d.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        dic={}\n",
    "        for i in range(len(source)):\n",
    "            for j in range(len(source[0])):\n",
    "                if source[i][j] not in dic:\n",
    "                    dic[source[i][j]]=1\n",
    "                else:\n",
    "                    dic[source[i][j]]+=1\n",
    "        res=0\n",
    "        for i in range(len(source)):\n",
    "            for j in range(len(source[0])):\n",
    "                if target[i][j] not in dic:\n",
    "                    res+=1\n",
    "                else:\n",
    "                    dic[target[i][j]]-=1\n",
    "                    if dic[target[i][j]]==0:\n",
    "                        del dic[target[i][j]]\n",
    "        \n",
    "        return res\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "\n",
    "        dic = defaultdict(int)\n",
    "        m = len(source)\n",
    "        n = len(source[0])\n",
    "        for i in range(m*n):\n",
    "            dic[source[i//n][i%n]] += 1\n",
    "        ans = 0\n",
    "        for j in range(m*n):\n",
    "            t = target[j//n][j%n]\n",
    "            if dic[t]>0:\n",
    "                dic[t] -= 1\n",
    "            else:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# from collections import defaultdict\n",
    "# class Solution:\n",
    "#     def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "#         dic=defaultdict(int)\n",
    "#         for row in source:\n",
    "#             for j in range(len(row)):\n",
    "#                 dic[row[j]]+=1\n",
    "        \n",
    "#         for row in target:\n",
    "#             for j in range(len(row)):\n",
    "#                 dic[row[j]]-=1\n",
    "#                 if dic[row[j]]==0:\n",
    "#                     del dic[row[j]]\n",
    "\n",
    "#         lst=[-v for k,v in dic.items() if v<0]\n",
    "\n",
    "#         return sum(lst) if lst else 0\n",
    "\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        dic=defaultdict(int)\n",
    "        for row in source:\n",
    "            for j in range(len(row)):\n",
    "                dic[row[j]]+=1\n",
    "        cnt=0\n",
    "        for row in target:\n",
    "            for j in range(len(row)):\n",
    "                dic[row[j]]-=1\n",
    "                if dic[row[j]]>=0:\n",
    "                    cnt+=1\n",
    "                    # del dic[row[j]]\n",
    "\n",
    "        # lst=[-v for k,v in dic.items() if v<0]\n",
    "        return len(source)*len(source[0])-cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        d = {}\n",
    "        for x in source:\n",
    "            for y in x:\n",
    "                if y in d:\n",
    "                    d[y] += 1\n",
    "                else:\n",
    "                    d[y] = 1\n",
    "\n",
    "        for x in target:\n",
    "            for y in x:\n",
    "                if y in d:\n",
    "                    d[y] -= 1\n",
    "                    if d[y] == 0:\n",
    "                        del d[y]\n",
    "                else:\n",
    "                    pass\n",
    "        #print(d)\n",
    "        return sum(d.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        cnt = Counter()\n",
    "        for row in source:\n",
    "            for x in row:\n",
    "                cnt[x] += 1\n",
    "        for row in target:\n",
    "            for x in row:\n",
    "                cnt[x] -= 1\n",
    "        return sum(abs(x) for x in cnt.values()) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        dic = {}\n",
    "        res = 0\n",
    "        for i in range(len(source)):\n",
    "            for j in range(len(source[0])):\n",
    "                n = source[i][j]\n",
    "                if n not in dic.keys():\n",
    "                    dic[n] = 1\n",
    "                else:\n",
    "                    dic[n] += 1\n",
    "        for i in range(len(target)):\n",
    "            for j in range(len(target[0])):\n",
    "                n = target[i][j]\n",
    "                if n in dic.keys():\n",
    "                    dic[n] -= 1\n",
    "        print(dic)\n",
    "        for k, v in dic.items():\n",
    "            if v > 0:\n",
    "                res += v\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "      d = {}\n",
    "      m, n = len(source), len(source[0])\n",
    "      for r in target:\n",
    "        for c in r:\n",
    "          d[c] = d.get(c, 0) + 1\n",
    "      k = 0\n",
    "      for r in source:\n",
    "        for c in r:\n",
    "          t = d.get(c, 0)\n",
    "          if t > 0:\n",
    "            d[c] -= 1\n",
    "            k += 1\n",
    "      return m * n - k\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        n, m = len(source), len(source[0])\n",
    "        freq = {}\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if source[i][j] not in freq:\n",
    "                    freq[source[i][j]] = 1\n",
    "                else:\n",
    "                    freq[source[i][j]] += 1\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if target[i][j] in freq:\n",
    "                    if freq[target[i][j]] == 1:\n",
    "                        freq.pop(target[i][j])\n",
    "                    else:\n",
    "                        freq[target[i][j]] -= 1\n",
    "        return sum(freq.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        cnt = Counter()\n",
    "        m, n = len(source), len(source[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                cnt[source[i][j]] += 1\n",
    "                cnt[target[i][j]] -= 1\n",
    "        return sum(abs(v) for v in cnt.values()) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        n, m = len(source), len(source[0])\n",
    "        \n",
    "        c = defaultdict(int)\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                c[source[i][j]] += 1\n",
    "                c[target[i][j]] -= 1\n",
    "        ans = 0\n",
    "        for t in c:\n",
    "            if c[t] > 0:\n",
    "                ans += c[t]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        dic=defaultdict(int)\n",
    "        for row in source:\n",
    "            for j in range(len(row)):\n",
    "                dic[row[j]]+=1\n",
    "        \n",
    "        for row in target:\n",
    "            for j in range(len(row)):\n",
    "                dic[row[j]]-=1\n",
    "                if dic[row[j]]==0:\n",
    "                    del dic[row[j]]\n",
    "\n",
    "        lst=[-v for k,v in dic.items() if v<0]\n",
    "\n",
    "        return sum(lst) if lst else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "### my：哈希\n",
    "# from collections import defaultdict\n",
    "# class Solution:\n",
    "#     def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "#         dic=defaultdict(int)\n",
    "#         for row in source:\n",
    "#             for j in range(len(row)):\n",
    "#                 dic[row[j]]+=1\n",
    "        \n",
    "#         for row in target:\n",
    "#             for j in range(len(row)):\n",
    "#                 dic[row[j]]-=1\n",
    "#                 if dic[row[j]]==0:\n",
    "#                     del dic[row[j]]\n",
    "\n",
    "#         lst=[-v for k,v in dic.items() if v<0]\n",
    "\n",
    "#         return sum(lst) if lst else 0\n",
    "\n",
    "### my:哈希+计数（refer 网友）\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        dic=defaultdict(int)\n",
    "        for row in source:\n",
    "            for j in range(len(row)):\n",
    "                dic[row[j]]+=1\n",
    "        cnt=0\n",
    "        for row in target:\n",
    "            for j in range(len(row)):\n",
    "                dic[row[j]]-=1\n",
    "                if dic[row[j]]>=0:\n",
    "                    cnt+=1\n",
    "                    # del dic[row[j]]\n",
    "\n",
    "        # lst=[-v for k,v in dic.items() if v<0]\n",
    "        return len(source)*len(source[0])-cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        return sum((Counter(chain(*source))-Counter(chain(*target))).values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(\n",
    "        self, source: list[list[int]], target: list[list[int]]\n",
    "    ) -> int:\n",
    "        ds: dict[int, int] = {}\n",
    "        for sub in source:\n",
    "            for i in sub:\n",
    "                ds[i] = ds.get(i, 0) + 1\n",
    "        dt: dict[int, int] = {}\n",
    "        for sub in target:\n",
    "            for i in sub:\n",
    "                dt[i] = dt.get(i, 0) + 1\n",
    "\n",
    "        count: int = 0\n",
    "        for i in dt.keys():\n",
    "            count += max(dt[i] - ds.get(i, 0),0)\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        source = collections.Counter(itertools.chain(*source))\n",
    "        target = collections.Counter(itertools.chain(*target))\n",
    "        same = 0\n",
    "        for k, v in source.items():\n",
    "            same += min(v, target.get(k, 0))\n",
    "        return sum(source.values()) - same\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        d = {}\n",
    "        d2 = {}\n",
    "        for w in source:\n",
    "            for h in w:\n",
    "                if h in d:\n",
    "                    d[h] += 1\n",
    "                else:\n",
    "                    d[h] = 1\n",
    "        for w in target:\n",
    "            for h in w:\n",
    "                if h in d2:\n",
    "                    d2[h] += 1\n",
    "                else:\n",
    "                    d2[h] = 1\n",
    "        r = 0\n",
    "        for k in d2:\n",
    "            if k not in d:\n",
    "                r += d2[k]\n",
    "            else:\n",
    "                if d[k] < d2[k]:\n",
    "                    r += d2[k] - d[k]\n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "#         import numpy as np\n",
    "#         # sourcecopy = source\n",
    "#         M = 0  #nums need change color\n",
    "#         A = len(target)\n",
    "#         B = len(target[-1])\n",
    "#         source_1 = list((np.array(source)).reshape(A*B,))\n",
    "#         target_1 = list((np.array(target)).reshape(A*B,))\n",
    "#         nums = len(target_1)\n",
    "#         # print('nums',target_1)\n",
    "#         for i in range(nums): #target 7\n",
    "#             M_tmp = 0\n",
    "#             for j in range(len(source_1)): \n",
    "#                 # print(i,j)\n",
    "#                 if target_1[i] == source_1[j]:\n",
    "#                     del source_1[j]\n",
    "#                     M_tmp = 1  # UAV不用变颜色\n",
    "#                     # print('source_1',source_1)\n",
    "#                     break              \n",
    "#             if M_tmp == 0:\n",
    "#                 M+=1\n",
    "#         print('M',M)\n",
    "#         return M\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        return sum((Counter(chain(*source))-Counter(chain(*target))).values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        # a = [1,2,3,4]\n",
    "        # for i in range(0,2):\n",
    "        #     a.pop(a.index(a[i]))\n",
    "        # print(a)\n",
    "        ls1 = []\n",
    "        ls2 = []\n",
    "        for i in range(0,len(source)):\n",
    "            for j in range(0,len(source[0])):\n",
    "                ls1.append(source[i][j])\n",
    "        ls1.sort()\n",
    "        print(ls1)\n",
    "        for i in range(0,len(target)):\n",
    "            for j in range(0,len(target[0])):\n",
    "                ls2.append(target[i][j])\n",
    "        ls2.sort()\n",
    "        print(ls2)\n",
    "        print(len(ls1),len(ls2))\n",
    "        cnt =0\n",
    "        c1 = Counter(ls1)\n",
    "        \n",
    "        for x in ls2:\n",
    "            c1[x] -= 1\n",
    "        return sum([x for x in c1.values() if x >=0])\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        d={}\n",
    "        dd={}\n",
    "        ans=0\n",
    "        m=len(source)\n",
    "        n=len(source[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if source[i][j] not in d:\n",
    "                    d[source[i][j]]=1\n",
    "                else:\n",
    "                    d[source[i][j]]+=1\n",
    "                if target[i][j] not in dd:\n",
    "                    dd[target[i][j]]=1\n",
    "                else:\n",
    "                    dd[target[i][j]]+=1\n",
    "        for i in list(d.keys()):\n",
    "            if not i in dd:\n",
    "                ans+=d[i]\n",
    "            else:\n",
    "                if d[i]>dd[i]:\n",
    "                    ans+=d[i]-dd[i]\n",
    "        return ans\n",
    "\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        return sum((Counter(chain(*source))-Counter(chain(*target))).values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        # 统计 source 中每种颜色出现的次数\n",
    "        source_counter = Counter()\n",
    "        for row in source:\n",
    "            for color in row:\n",
    "                source_counter[color] += 1\n",
    "        \n",
    "        # 统计 target 中每种颜色出现的次数\n",
    "        target_counter = Counter()\n",
    "        for row in target:\n",
    "            for color in row:\n",
    "                target_counter[color] += 1\n",
    "        \n",
    "        # 计算需要切换的无人机数量\n",
    "        switches = 0\n",
    "        for color, count in source_counter.items():\n",
    "            # 如果这种颜色在 target 中也出现，那么我们可以通过布局调整来匹配它们，无需切换\n",
    "            # 否则，所有这种颜色的无人机都需要切换\n",
    "            switches += max(0, count - target_counter.get(color, 0))\n",
    "        \n",
    "        return switches\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        return sum((Counter(chain(*source))-Counter(chain(*target))).values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from itertools import chain\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        return sum((Counter(chain(*source)) - Counter(chain(*target))).values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        cnt_s = Counter(sum(source,[]))\n",
    "        cnt_t = Counter(sum(target,[]))\n",
    "        return sum((cnt_t - cnt_s).values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        return sum((Counter(chain(*source))-Counter(chain(*target))).values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        from collections import Counter\n",
    "        sc = Counter(sum(source, []))\n",
    "        tc = Counter(sum(target, []))\n",
    "        res = 0\n",
    "        for i in sc.keys():\n",
    "            res += max(sc[i]-tc[i], 0)\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        s=[]\n",
    "        t=[]\n",
    "        for i in source:\n",
    "            s+=i\n",
    "        for i in target:\n",
    "            t+=i\n",
    "        s=collections.Counter(s)\n",
    "        t=collections.Counter(t)\n",
    "        a=(s-t).values()\n",
    "        return sum(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        return sum((Counter(chain(*source))-Counter(chain(*target))).values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        return sum((Counter(chain(*source))-Counter(chain(*target))).values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        # dic = collections.defaultdict(int)\n",
    "        # c1 = collections.Counter()\n",
    "        # for item in source:\n",
    "        #     c2 = collections.Counter(item)\n",
    "        #     for k in c2:\n",
    "        #         c1[k] += c2[k]\n",
    "        # for item in target:\n",
    "        #     c2 = collections.Counter(item)\n",
    "        #     for k in c2:\n",
    "        #         if k in c1.keys():\n",
    "        #             c1[k] -= c2[k]\n",
    "        # res = 0\n",
    "        # for k in c1:\n",
    "        #     res += c1[k]\n",
    "        # return res\n",
    "        s = []\n",
    "        t = []\n",
    "        for item in source:\n",
    "            s += item\n",
    "        for item in target:\n",
    "            t += item\n",
    "        c1 = collections.Counter(s)\n",
    "        c2 = collections.Counter(t)\n",
    "        res = 0\n",
    "        for k in c2.keys():\n",
    "            if k in c1.keys():\n",
    "                c1[k] -= c2[k]\n",
    "        for k in c1.keys():\n",
    "            if c1[k]>0:\n",
    "                res += c1[k]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        lst1, lst2 = {}, {}\n",
    "        for i in source:\n",
    "            for j in i:\n",
    "                if j not in lst1:\n",
    "                    lst1[j] = 1\n",
    "                else:\n",
    "                    lst1[j] += 1\n",
    "\n",
    "        for i in target:\n",
    "            for j in i:\n",
    "                if j not in lst2:\n",
    "                    lst2[j] = 1\n",
    "                else:\n",
    "                    lst2[j] += 1\n",
    "        \n",
    "        print(lst1)\n",
    "        print(lst2)\n",
    "\n",
    "        res = 0\n",
    "        for key,val in lst1.items():\n",
    "            if key in lst2:\n",
    "                print(key,val,lst2[key])\n",
    "                res += max(val - lst2[key],0)\n",
    "            else:\n",
    "                res += val\n",
    "                print(key,val)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        return sum((Counter([x for y in source for x in y])-Counter([x for y in target for x in y])).values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        set_s={}\n",
    "        set_t={}\n",
    "        res=0\n",
    "        for i in range(len(source)):\n",
    "            for j in range(len(source[0])):\n",
    "                if source[i][j] not in set_s:\n",
    "                    set_s[source[i][j]]=1\n",
    "                else:\n",
    "                    set_s[source[i][j]]+=1\n",
    "                if target[i][j] not in set_t:\n",
    "                    set_t[target[i][j]]=1\n",
    "                else:\n",
    "                    set_t[target[i][j]]+=1\n",
    "        print(set_s)\n",
    "        print(set_t)\n",
    "        for i in set_s.keys():\n",
    "            if i in set_t.keys():\n",
    "                res+=min(set_s[i],set_t[i])\n",
    "                # print(res)\n",
    "        return len(source)*len(source[0])-res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        source2=[]\n",
    "        for i in source:\n",
    "            for j in i:\n",
    "                source2.append(j)\n",
    "        \n",
    "        \n",
    "        target2=[]\n",
    "        for i in target:\n",
    "            for j in i:\n",
    "                target2.append(j)\n",
    "        aa=Counter(source2)\n",
    "        bb=Counter(target2)\n",
    "        total=0\n",
    "        for i,j in bb.items():\n",
    "            if i in aa:\n",
    "                temp=0 if aa[i]>=j else j-aa[i]\n",
    "                total+=temp \n",
    "            else:\n",
    "                total+=j \n",
    "        return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c1, c2, res = Counter(sum(source, [])),  Counter(sum(target, [])), 0\n",
    "        for i in set(c1): res += max(c1[i]-c2[i], 0)\n",
    "        return res\n",
    "    \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        from collections import Counter\n",
    "        sc = Counter(sum(source, []))\n",
    "        tc = Counter(sum(target, []))\n",
    "        res = 0\n",
    "        for i in set(sc): \n",
    "            res += max(sc[i]-tc[i], 0)\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c1, c2, res = Counter(sum(source,[])), Counter(sum(target,[])), 0\n",
    "        for i in set(c1): \n",
    "          res += max(c1[i]-c2[i],0)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c1, c2, res = Counter(sum(source,[])), Counter(sum(target,[])), 0\n",
    "#sum(二维列表,[])可以使列表降维\n",
    "        for i in set(c1): \n",
    "          res += max(c1[i]-c2[i],0)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c1, c2, res = Counter(sum(source,[])), Counter(sum(target,[])), 0\n",
    "        for i in set(c1): res += max(c1[i]-c2[i],0)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        from collections import Counter\n",
    "        sc = Counter(sum(source, []))\n",
    "        tc = Counter(sum(target, []))\n",
    "        res = 0\n",
    "        for i in set(sc):\n",
    "            res += max(sc[i]-tc[i], 0)\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c1, c2, res = Counter(sum(source,[])), Counter(sum(target,[])), 0\n",
    "        print(set(c1))\n",
    "#sum(二维列表,[])可以使列表降维\n",
    "        for i in set(c1): \n",
    "          res += max(c1[i]-c2[i],0)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c1 = collections.Counter(sum(target, []))\n",
    "        c2 = collections.Counter(sum(source, []))\n",
    "        return sum(abs(c1[i]-c2[i]) for i in c1.keys()|c2.keys())//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        # source = [[1,3],\n",
    "        #           [5,4]], \n",
    "\n",
    "        # target = [[3,1],\n",
    "        #           [6,5]]\n",
    "\n",
    "        #  求最少需要多少架无人机切换灯光颜色\n",
    "\n",
    "        n = len(source)                # 表示长\n",
    "        m = len(source[0])             # 表示宽\n",
    "\n",
    "        Mydict1 = dict()\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if source[i][j] not in Mydict1:\n",
    "                    Mydict1[source[i][j]] = 1\n",
    "                else:\n",
    "                    Mydict1[source[i][j]] += 1\n",
    "        new_sys1 = sorted(Mydict1.items(), key=lambda d: d[0], reverse=False)\n",
    "        print(new_sys1)\n",
    "        \n",
    "        Mydict2 = dict()\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if target[i][j] not in Mydict2:\n",
    "                    Mydict2[target[i][j]] = 1\n",
    "                else:\n",
    "                    Mydict2[target[i][j]] += 1\n",
    "        new_sys2 = sorted(Mydict2.items(), key=lambda d: d[0], reverse=False)\n",
    "        print(new_sys2)\n",
    "\n",
    "        count = 0\n",
    "        for key in Mydict2:\n",
    "            if key in Mydict1:\n",
    "                if Mydict2[key] > Mydict1[key]:\n",
    "                    count += Mydict2[key] - Mydict1[key]\n",
    "            else:  \n",
    "                print(key)                      # 在里面的\n",
    "                count += Mydict2[key]\n",
    "        return count \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c1, c2, res = Counter(sum(source,[])), Counter(sum(target,[])), 0\n",
    "        for i in set(c2)|set(c1): res += abs(c2[i]-c1[i])\n",
    "        return res//2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c1, c2, res = Counter(sum(source,[])), Counter(sum(target,[])), 0\n",
    "        for i in set(c2)|set(c1): res += abs(c2[i]-c1[i])\n",
    "        return res//2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c1,c2,res = Counter(sum(source,[])),Counter(sum(target,[])),0 #c1 c2是两个字典 统计两个列表中不同元素的个数 key是元素 value是个数\n",
    "        for i in set(c1)|set(c2):\n",
    "            res += abs(c1[i]-c2[i])\n",
    "        return res//2\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        ans = 0\n",
    "        cnt_s = Counter(sum(source,[]))\n",
    "        cnt_t = Counter(sum(target,[]))\n",
    "        for i in set(cnt_s)|set(cnt_t):\n",
    "            ans += abs(cnt_s[i] - cnt_t[i])\n",
    "        return ans//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c1,c2,res = Counter(sum(source,[])),Counter(sum(target,[])),0 #c1 c2是两个字典 统计两个列表中不同元素的个数 key是元素 value是个数\n",
    "        for i in set(c1)|set(c2):\n",
    "            res += abs(c1[i]-c2[i])\n",
    "        return res//2\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "       c1=Counter(sum(source,[]))\n",
    "       c2=Counter(sum(target,[]))\n",
    "       res=0\n",
    "       for i in set(c2)|set(c1):\n",
    "          res=res+abs(c2[i]-c1[i])\n",
    "       return res//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "#         import numpy as np\n",
    "#         # sourcecopy = source\n",
    "#         M = 0  #nums need change color\n",
    "#         A = len(target)\n",
    "#         B = len(target[-1])\n",
    "#         source_1 = list((np.array(source)).reshape(A*B,))\n",
    "#         target_1 = list((np.array(target)).reshape(A*B,))\n",
    "#         nums = len(target_1)\n",
    "#         # print('nums',target_1)\n",
    "#         for i in range(nums): #target 7\n",
    "#             M_tmp = 0\n",
    "#             for j in range(len(source_1)): \n",
    "#                 # print(i,j)\n",
    "#                 if target_1[i] == source_1[j]:\n",
    "#                     del source_1[j]\n",
    "#                     M_tmp = 1  # UAV不用变颜色\n",
    "#                     # print('source_1',source_1)\n",
    "#                     break              \n",
    "#             if M_tmp == 0:\n",
    "#                 M+=1\n",
    "#         print('M',M)\n",
    "#         return M\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        c1, c2, res = Counter(sum(source,[])), Counter(sum(target,[])), 0\n",
    "        for i in set(c2)|set(c1): res += abs(c2[i]-c1[i])\n",
    "        return res//2\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSwitchingTimes(self, source: List[List[int]], target: List[List[int]]) -> int:\n",
    "        s=[]\n",
    "        t=[]\n",
    "        ans=0\n",
    "        for i in source:\n",
    "            s.extend(i)\n",
    "        for i in target:\n",
    "            t.extend(i)\n",
    "        cnt1=Counter(s)\n",
    "        cnt2=Counter(t)\n",
    "        for i in set(cnt1)|set(cnt2):\n",
    "            ans+=abs(cnt1[i]-cnt2[i])\n",
    "        return ans//2"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
